package com.wavehi.producer;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.wavehi.util.RabbitMQFactoryUtils;
import com.wavehi.util.SleepUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 生产者 演示死信队列的使用
 * @Author WaveHi
 * @Date 27/12/2021 下午 10:03
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 */
public class Producer_01 {

    //普通交换机名称
    private static final String NORMAL_EXCHANGE = "normal_exchange";

    public static void main(String[] args) throws Exception {

        //1.获取连接信道
        Channel channel = RabbitMQFactoryUtils.getChannel();
        //2.准备消息
        List<String> list =
                Arrays.asList("Wow!", "Hello!", "Yes!", "Hi!", "No!", "Good!", "Go!", "Ok!");
        //3.发送消息
        //3.1.设置消息的存活时间
        AMQP.BasicProperties properties =
                new AMQP.BasicProperties()
                        .builder()
                        .expiration("10000")
                        .build();
        list.forEach(s -> {
            //3.2.发送
            SleepUtils.sleep(3);
            try {
                channel.basicPublish(NORMAL_EXCHANGE, "zhangsan", properties,
                        s.getBytes("UTF-8"));
                System.out.printf("Send message '%s'.\n", s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}
